<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button v-auths="['fault@add','admin']" type="primary" icon="el-icon-plus" @click="add">申告</el-button>
				<!-- <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0"
					@click="batch_del"></el-button> -->
			</div>
			<div class="right-panel">
				<el-date-picker v-model="date" type="daterange" value-format="YYYY-MM-DD" range-separator="至"
					start-placeholder="开始日期" end-placeholder="结束日期" @change="changeDate"
					style="width:20%"></el-date-picker>
				<el-select v-model="searchData.hitchType" clearable placeholder="故障类型" @change="upsearch"
					style="width:15%">
					<el-option v-for="item in $DICT.hitchTypeSelect" :key="item.id" :label="item.label"
						:value="item.value" />
				</el-select>
				<el-input v-model="searchData.keyword" placeholder="故障描述/联系电话" clearable style="width:20%"></el-input>

				<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" :params="params" orderBy="sort" row-key="id"
				@selection-change="selectionChange" stripe :hideDo="true">
				<el-table-column type="selection" width="50" align="center"></el-table-column>
				<el-table-column label="故障类型" prop="hitchType" align="center">
				</el-table-column>
				<el-table-column label="故障描述" prop="describeText" align="center">
				</el-table-column>
				<el-table-column label="申告人" prop="createByName" align="center"></el-table-column>
				<el-table-column label="联系电话" prop="linkTel" align="center"></el-table-column>
				<el-table-column label="申告时间" prop="createTime" align="center"></el-table-column>
				<el-table-column label="处理状态" prop="handleStatus" align="center">
					<template #default="scope">
						{{$DICT.handleStatus[scope.row.handleStatus]}}
					</template>
				</el-table-column>
				<el-table-column label="处理结果" prop="conclusion" align="center">
					<template #default="scope">
						<el-popover placement="bottom" title="处理结果" :width="400" trigger="hover"
							:content="scope.row.conclusion">
							<template #reference>
								<el-button size="small">查看</el-button>
							</template>
						</el-popover>
					</template>
				</el-table-column>
				<el-table-column label="发起时间" prop="createTime" align="center"></el-table-column>
				<el-table-column label="操作" fixed="right" align="center" min-width="140">
					<template #default="scope">
						<el-button v-role="['SAQ','admin']" plain size="small"
							@click="table_manager(scope.row)">查看</el-button>
						<el-button v-auths="['fault@edit','admin']" type="primary" plain size="small" @click="table_edit(scope.row)">派单</el-button>


						<!-- <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button plain type="danger" size="small">删除</el-button>
							</template>
						</el-popconfirm> -->
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess"
		@closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './save'
	export default {
		name: 'store',
		components: {
			saveDialog
		},
		data() {
			return {
				dialog: {
					save: false,
				},
				date: [],
				searchData: {
					startDate: '',
					endDate: ''
				},
				apiObj: this.$API.fault.hitch.dispatch,
				params: {},
				selection: []

			}
		},
		mounted() {
			console.log(this.$DICT.statusDictSelect)
		},
		methods: {
			//搜索
			upsearch() {
				this.$refs.table.upData(this.searchData)
			},
			// 日期筛选
			changeDate(e) {
				if (e) {
					this.searchData.startDate = e[0]
					this.searchData.endDate = e[1]
				} else {
					this.searchData.startDate = ''
					this.searchData.endDate = ''
				}

				this.$refs.table.upData(this.searchData)
			},
			//窗口新增
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//窗口编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//窗口编辑
			table_qrcode(row) {
				this.dialog.qrcode = true
				this.$nextTick(() => {
					this.$refs.qrcodeDialog.open().setData(row)
				})
			},
			table_device(row) {
				this.dialog.device = true
				this.$nextTick(() => {
					this.$refs.deviceDialog.open().setData(row)
				})
			},
			//页面新增
			addPage() {
				this.$router.push({
					path: '/template/list/crud/detail',
				})
			},
			//页面编辑
			table_edit_page(row) {
				this.$router.push({
					path: '/template/list/crud/detail',
					query: {
						id: row.id
					}
				})
			},
			//查看
			table_show(row) {
				this.dialog.info = true
				this.$nextTick(() => {
					this.$refs.infoDialog.setData(row)
				})
			},
			table_manager(row) {
				this.dialog.manager = true
				this.$nextTick(() => {
					this.$refs.managerDialog.setData(row)
				})
			},
			table_managerAdmin(row) {
				this.dialog.managerAdmin = true
				this.$nextTick(() => {
					this.$refs.managerAdminDialog.setData(row)
				})
			},
			//删除明细
			async table_del(row, index) {
				var reqData = {
					id: row.id
				}
				var res = await this.$API.fault.hitch.delete.post(reqData);
				if (res.code == 200) {
					this.$refs.table.removeIndex(index)
					this.$message.success("删除成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				var confirmRes = await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '删除',
					confirmButtonClass: 'el-button--danger'
				}).catch(() => {})

				if (!confirmRes) {
					return false
				}

				var ids = this.selection.map(v => v.id)
				let id = ids.join(",")

				const res = this.$API.contract.insure.delete.post({
					id: id
				})
				if (res) {
					this.$message.success("操作成功")
					this.$refs.table.removeKeys(ids)
					//this.$refs.table.upData()
				}

			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection
			},
			//本地更新数据
			handleSaveSuccess(data, mode) {
				//为了减少网络请求，直接变更表格内存数据
				if (mode == 'add') {
					this.$refs.table.unshiftRow(data)
				} else if (mode == 'edit') {
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			}
		}
	}
</script>

<style>
</style>
